package com.hhm.controller;

import com.github.pagehelper.PageInfo;
import com.hhm.pojo.User;
import com.hhm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController {
    @Autowired
    private UserService us;

   @ResponseBody
   @RequestMapping("updateUser")
   public boolean updateUser(User user){
        int i = us.updateUser(user);
        if (i>0){
            return true;
       }
        return false;
    }

    /**
     * 回显
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("selectOne")
    public String selectOne(Model model,Integer id){
        User user = us.selectOne(id);
        model.addAttribute(user);
        return "update";
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("del")
    public boolean del(Integer id){
        int i = us.del(id);
        if (i>0){
            return true;
        }
        return false;
    }

    /**
     * 列表
     * @param user
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @RequestMapping("list")
    public String list(User user, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "3") Integer pageSize, Model model){
        PageInfo<User> pageInfo = us.findAllByPage(pageNum, pageSize, user);

        model.addAttribute("pg", pageInfo);
        model.addAttribute("user", user);

        return "list";
    }
}
